<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>


<body>
    <div class="denglu">
        <form id="dataForm">

            <div class="email_group">
                邮箱(请使用国内邮箱)<br>
                <input type="text" id="email" name="email"><input type="button" value="获取验证码" id="getVerificationCodes">
            </div>
    </div>

    <pre id="output">注册消息(记住自己邮箱和验证码)</pre>

    <a href="./register.html"> 注册页面</a>
    <a href="./login.html"> 登录页面</a>
    <a href="./chagePass.html"> 修改密码页面</a>
    </form>

    </div>
    


</body>
<script>
    let codeIndex = false;
    let emailTimeOut = 0;
    let time = 0;

    function isValidEmail(email) {
        //正则表达式（Regular Expressions，简称 regex）是一个强大的文本处理工具，用于匹配字符串中的特定模式。它通常用于验证、查找、替换字符串，或者从文本中提取数据。
        const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
        return regex.test(email);
    }


    


    const getVerificationCodes = document.getElementById('getVerificationCodes');

    getVerificationCodes.addEventListener('click', async function (event) {
        event.preventDefault();  // 阻止表单默认提交

        const email = document.getElementById('email').value;

        try {
            if (emailTimeOut <= 0) {
                if (!email) {
                    document.getElementById("output").textContent = "邮箱不能为空";
                }
                if (!isValidEmail(email)) {
                    document.getElementById("output").textContent = "邮箱格式不正确";
                }

                else if (email && isValidEmail(email)) {
                    const response = await fetch("http://124.221.110.118:3000/send_verificationCode", {
                        method: "POST",
                        headers: {
                            "Content-Type": "application/json"
                        },
                        body: JSON.stringify({ email: email })
                    });
                    if (!response.ok) {
                        throw new Error('网络错误或验证码发送失败');
                    }

                    const result = await response.json();


                    if (response.status === 200) {
                        document.getElementById("output").textContent = "验证码已发送，请检查邮箱";
                        emailTimeOut = 60;
                        setTimeout(() => {
                            emailTimeOut = 0;
                            //window.location.href = "./register.html"
                        }, 60000);
                    }
                    if (response.status === 400) {
                        document.getElementById("output").textContent = "验证码获取时间间隔太短，请稍后再试";
                    }
                }
            }
            else {
                document.getElementById("output").textContent = "发送验证码冷却中，时长5min";
            }

        }
        catch (error) {
            console.error("请求失败", error);
            document.getElementById("output").textContent = "网络错误，发送失败！";
        }
    })

    
</script>

</html>